export function takeFontSize (to) {
  rem();

  // 基于Iphone6
  //console.log('初始的font-size：' + getCookie('HSH_SIZE').split('p')[0])
  if (to.meta.fontSize6) {
    var Num = to.meta.fontSize6 / 57.8704
    var fontSize = getCookie('HSH_SIZE').split('p')[0] * Num
    //console.log('改变后的font-size：' + fontSize)
    // eslint-disable-next-line no-undef
    setCookie('HSH_SIZE_NEW', fontSize)
  } else {
    // eslint-disable-next-line no-redeclare
    var fontSize = getCookie('HSH_SIZE').split('p')[0] * 1
    //console.log('改变后的font-size：' + fontSize)
    // eslint-disable-next-line no-undef
    setCookie('HSH_SIZE_NEW', fontSize)
  }
}

export function checkpagehtml () {
  // eslint-disable-next-line promise/param-names
  return new Promise((resolve) => {
    var NfontSize = Math.round(getCookie('HSH_SIZE_NEW') * 100) / 100 + 'px'
    // document.getElementsByTagName('html')[0].style='font-size:' + getCookie('projectStyle1') + 'px';
    document.getElementsByTagName('html')[0].style.fontSize = NfontSize
    //console.log('即将改变的fontSize：' + NfontSize)
    //console.log('页面显示的fontSize：' + document.getElementsByTagName('html')[0].style['font-size'])
    resolve
  })
}

export function lastcheckpage (NfontSize) {
  if (document.getElementsByTagName('html')[0].style['font-size'] != NfontSize) {
    document.getElementsByTagName('html')[0].style.fontSize = NfontSize
  }
}

function getCookie (name) {
  // eslint-disable-next-line one-var
  var arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
  // eslint-disable-next-line no-irregular-whitespace
  return (arr = document.cookie.match(reg)) ? unescape(arr[2]) : null
}

function rem () {
  +function(win, doc) {
    'use strict';
    var options = { width: 648, dpr: 2 };
    // eslint-disable-next-line one-var
    var html = doc.documentElement,
        width = html.getAttribute('data-width') || options.width,
        dpr = html.getAttribute('data-dpr') || options.dpr,
        viewPort = doc.querySelector('meta[name="viewport"]'),
        rotate = win.onorientationchange ? 'orientationchange' : 'resize';

    function setSize () {
      var winWidth = win.innerWidth || html.clientWidth
      var projectStyle = Math.round((100 * winWidth / width) * 10000) / 10000 + 'px'
      // console.log("woaini" + projectStyle);
      //console.log('woshishei' + projectStyle)
      setCookie('HSH_SIZE', projectStyle)
    };
    win.addEventListener(rotate, setSize)
    window.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame;
    setSize()
    requestAnimationFrame(function () {
      setSize()
    })
  }(window, document)
}